namespace ScrumSprintMonitor.UI.Wpf
{
	#region #using Directives

	using System;
	using System.Text;
	using System.Windows;

	using Implementation;

	using Infrastructure;

	using Microsoft.Practices.ServiceLocation;

	using ViewModels;

	#endregion

	public class XamlServiceLocator
	{
		static XamlServiceLocator()
		{
			if (DesignerProperties.GetIsInDesignMode(null))
			{
				UnityContainerAbstraction.RegisterModule<UnityUiDesignTimeModule>();
			}

			UnityContainerAbstraction.EnsureContainerIsSetUp();
		}

		public IMonitorViewModel MonitorViewModel
		{
			get { return Get<IMonitorViewModel>(); }
		}

		public ISprintHoursChartViewModel SprintHoursChartViewModel
		{
			get { return Get<ISprintHoursChartViewModel>(); }
		}

		public IBacklogItemCountChartViewModel BacklogItemCountChartViewModel
		{
			get { return Get<IBacklogItemCountChartViewModel>(); }
		}

		public IUserStoryCountChartViewModel UserStoryCountChartViewModel
		{
			get { return Get<IUserStoryCountChartViewModel>(); }
		}

		public ITeamMembersListViewModel TeamMembersListViewModel
		{
			get { return Get<ITeamMembersListViewModel>(); }
		}

		public static T Get<T>()
		{
			try
			{
				return ServiceLocator.Current.GetInstance<T>();
			}
			catch (Exception ex)
			{
				var message = new StringBuilder();
				do
				{
					message.AppendLine(ex.Message);

					ex = ex.InnerException;
					if (ex != null)
					{
						message.AppendLine();
						message.AppendLine("Inner Exception:");
					}
				} while (ex != null);

				MessageBox.Show(message.ToString());

				throw;
			}
		}
	}
}